//
//  TPArticleDetailFooter.m
//  Yimei
//
//  Created by xiaowu xiaowu on 2017/10/31.
//  Copyright © 2017年 xiaowu xiaowu. All rights reserved.
//

#import "TPArticleDetailFooter.h"
//#import "UITextField+XWKit.h"
@interface TPArticleDetailFooter()<UITextFieldDelegate>
@property (nonatomic,strong)UITextField *textField;
@end
@implementation TPArticleDetailFooter
-(instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
//        [self setup];
  }
    return self;
}
-(instancetype)init{
    if(self = [super init]){
        [self setup];
    }
    return self;
}
-(void)setup{
    
    self.backgroundColor = TPAllBackgroundColor;
    
    _textField = [[UITextField alloc]init];
    _textField.placeholder = @"请输入内容";
    [_textField setValue:[UIColor lightTextColor] forKeyPath:@"_placeholderLabel.textColor"];

    _textField.delegate = self;
//    _textField.borderStyle = UITextBorderStyleRoundedRect;
    [_textField xw_addLeftViewFrame:CGRectMake(0, 0, 10.0, 38.0)];
    [_textField xw_addRightViewFrame:CGRectMake(0, 0, 10.0, 38.0)];
    ViewBorderRadius(_textField, 38/2, 1.0, [UIColor whiteColor]);
    _textField.backgroundColor = [UIColor clearColor];
    _textField.tintColor = _textField.textColor = [UIColor whiteColor];
    
    [self addSubview:_textField];
    
    [self.titleButton setTitle:@"评论" forState:0];
    [self.titleButton addTouchUpInsideTarget:self action:@selector(actionButton:)];
    self.titleButton.titleLabel.font = fontSystemOfSize(16);
    [self.titleButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.titleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(0.0);
        make.height.mas_equalTo(self.mas_height);
        make.width.mas_equalTo(70.0);
        make.bottom.mas_equalTo(0.0);
    }];
    
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.titleButton.mas_left);
        make.left.mas_equalTo(20.0);
        make.centerY.mas_equalTo(self);
        make.height.mas_equalTo(38.0);
    }];
    
    
    @weakify(self);
    [[[NSNotificationCenter defaultCenter]rac_addObserverForName:UIKeyboardWillShowNotification object:nil]subscribeNext:^(NSNotification * _Nullable x) {
        @strongify(self);
        CGRect keyBoardFrame = [[x.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
        //获取键盘的动画持续时间
        CGFloat keyBoardDuration = [x.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(-keyBoardFrame.size.height);
        }];
        [UIView animateWithDuration:keyBoardDuration animations:^{
            
            //            [self getTextFieldBackground].bottom = keyBoardFrame.origin.y;
            [self.superview layoutIfNeeded];
            
        }];
    }];
    [[[NSNotificationCenter defaultCenter]rac_addObserverForName:UIKeyboardWillHideNotification object:nil]subscribeNext:^(NSNotification * _Nullable x) {
        @strongify(self);
        CGFloat keyBoardDuration = [x.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(0.0);
        }];
        [UIView animateWithDuration:keyBoardDuration animations:^{
            
            [self.superview layoutIfNeeded];
            
            
        }];
    }];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
-(void)actionButton:(UIButton *)sender{
    
    [_textField resignFirstResponder];
    if(self.didBlock){
        self.didBlock(sender, self.textField);
    }
}
@end
